<?php
/**
 * 首页
 */
require_once'includes/global.php';
require_once(ROOT_PATH . 'languages/' . $config['site_language'] . '/front.php');
require_once'includes/front.php';
set_online(create_uri("index"));
$smarty = new smarty();
smarty_header(true);
$cache_id = sprintf('%X', crc32(ROOT_PATH));
if (!$smarty->is_cached('index.html', $cache_id)) {
    $smarty->assign('link', get_link());
    $smarty->assign("imgswidth",count(get_imgs_homepage())*960);
    $smarty->assign("imgs",get_imgs_homepage());//图片轮播
    $smarty->assign("news_index",get_news_index());//显示在首页的新闻
    $smarty->assign("category",  get_allcategory());//所有分类
    $smarty->assign("product_cate",get_pro_category());//首页产品类别
    $smarty->assign("newsone",get_onenews_home());
    
    $smarty->assign("anlis",get_anli_home());
    $smarty->assign("anliname",get_anli_catename());
}
$smarty->display('index.html', $cache_id);

function get_anli_home(){
    $arr_category_id = array(13,14,15,16,17);
    $arr_anli = array();
    $arr_tmp = array();
    foreach($arr_category_id as $id){
        $res = $GLOBALS['db']->getall("select * from ". $GLOBALS['db_prefix'] ."content where category_id=$id and channel_id=2 ORDER BY content_is_best DESC , content_id DESC limit 4");
        $arr_anli[$id]=  $res;
    }
    return $arr_anli;
}

function get_onenews_home(){//来张有图的在首页显示
    $array = array();
    $res = $GLOBALS['db']->getall("select * from ". $GLOBALS['db_prefix'] ."content where channel_id = 1 and content_state=1 and content_thumb!='' order by  content_is_best=1 desc, content_id desc limit 1");
    if($res){
        $i=0;
        foreach($res as $item){
            $array[$i]['id'] = $item['content_id'];
            $array[$i]['img'] = "uploads/".$item['content_thumb'];
            $array[$i]['url'] = "content.php?id=".$item['content_id'];
            $i++;
        }
    }
    return $array[0];
}

function get_anli_catename(){
    $array = array();
    $res = $GLOBALS['db']->getall("select * from  ". $GLOBALS['db_prefix'] ."content_category where category_id in (13,14,15,16,17)");
    if($res){
        foreach($res as $item){
            $array[$item['category_id']]['category_id'] = $item['category_id'];
            $array[$item['category_id']]['category_name'] = $item['category_name'];
        }
    }
    return $array;
}

//图片轮播
function get_imgs_homepage(){
    $res = $GLOBALS['db']->getall("select * from ". $GLOBALS['db_prefix'] ."others where name='imgshomepage'");
    $array = array();
    if($res){
        $imgs = unserialize($res[0]['value']);
        if($imgs){
            $array = $imgs;
        }
    }
    return $array;
}

function get_pro_category() {
    $sql = "select * from ". $GLOBALS['db_prefix'] ."content where category_id = 5 and content_state=1 limit 4";
    $res = $GLOBALS['db']->getall($sql);
    $array = array();
    $i = 0;
    foreach($res as $row) {
        $array[$i]['title'] = $row['content_title'];
        $array[$i]['url'] = $row['content_url'];//链接到哪里
        $array[$i]['img'] = "uploads/".$row['content_thumb'];
        $array[$i]['content'] = $row['content_text'];
        $i++;
    }
    return $array;
}

//显示在首页的新闻   channel_id 要变
function get_news_index() {
    $sql = "select * from ". $GLOBALS['db_prefix'] . "content where channel_id = 1 and content_is_best=1 and content_state=1 order by content_id desc limit 5";
    $res = $GLOBALS['db']->getall($sql);
    $array = array();
    $i = 0;
    foreach ($res as $row) {
        $array[$i]['id'] = $row['channel_id'];
        $array[$i]['title'] = $row['content_title'];
        $array[$i]['cate'] = get_category_name($row['category_id']);
        $array[$i]['content_id'] = $row['content_id'];
        $array[$i]['date'] = date("Y-m-d", $row['content_time']);
        $array[$i]['url']=create_uri('content',array('id'=>$row['content_id']));
        $array[$i]['cate_url']=create_uri('channel',array('id'=>$row['channel_id'],'category_id'=>$row['category_id']));
        $i++;
    }
    return $array;
}

//分类
function get_allcategory() {
    $sql = "select * from ". $GLOBALS['db_prefix'] . "content_category ";
    $res = $GLOBALS['db']->getall($sql);
    $array = array();
    foreach ($res as $row) {
        $array[$row['category_id']] = $row['category_name'];
    }
    return $array;
}

function get_link() {
    $array = array();
    $res = $GLOBALS['db']->getall("SELECT * FROM " . $GLOBALS['db_prefix'] . "link WHERE link_state=1 ORDER BY link_sort ASC,link_id ASC");
    if ($res) {
        foreach ($res as $row) {
            $array[$row['link_id']]['id'] = $row['link_id'];
            $array[$row['link_id']]['name'] = $row['link_name'];
            $array[$row['link_id']]['logo'] = $row['link_logo'];
            $array[$row['link_id']]['text'] = $row['link_text'];
            $array[$row['link_id']]['url'] = $row['link_url'];
        }
    }
    return $array;
}

?>